package com.shrct.businessprocess.project.impl;

import com.google.common.base.Objects;
import com.google.common.collect.Iterables;
import com.shrct.businessprocess.budget.RCBudget;
import com.shrct.businessprocess.budget.RCCostBreakDown;
import com.shrct.businessprocess.diagnose.IDiagnoseAcceptor;
import com.shrct.businessprocess.money.Moneys;
import com.shrct.businessprocess.organization.RCEnterprise;
import com.shrct.businessprocess.organization.impl.RCEnterpriseImpl;
import com.shrct.businessprocess.product.RCIdentifiablePhysicalProductEntry;
import com.shrct.businessprocess.product.RCProductEntry;
import com.shrct.businessprocess.product.RCProductServiceTracer;
import com.shrct.businessprocess.project.RCCustomerContract;
import com.shrct.businessprocess.project.RCCustomerContractMoneyAccept;
import com.shrct.businessprocess.project.RCEarlyBuildInfo;
import com.shrct.businessprocess.project.RCExchangeRate;
import com.shrct.businessprocess.project.RCProject;
import com.shrct.businessprocess.project.RCProjectMoney;
import com.shrct.businessprocess.project.RCProjectStat;
import com.shrct.businessprocess.project.RCRealMoney;
import com.shrct.businessprocess.project.impl.RCCustomerContractMoneyAcceptImpl;
import com.shrct.businessprocess.project.impl.RCProjectImpl;
import com.shrct.businessprocess.project.proxy.RCCustomerContractProxy;
import com.shrct.businessprocess.purchase.RCBaseProductPurchaseBudget;
import com.shrct.businessprocess.purchase.RCBudgetContract;
import com.shrct.businessprocess.purchase.RCBudgetContractItem;
import com.shrct.businessprocess.purchase.RCHasPurchaseBudget;
import com.shrct.businessprocess.purchase.RCPurchaseIncomeRequest;
import edu.fudan.mylang.common.NumberExtensions;
import edu.fudan.mylang.pf.BaseModelObject;
import edu.fudan.mylang.pf.IGenericQuery;
import edu.fudan.mylang.pf.IObjectFactory;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.persistence.AssociationOverrides;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.eclipse.xtext.xbase.lib.DoubleExtensions;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.Functions.Function2;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.hibernate.annotations.DiscriminatorOptions;

@Entity
@DiscriminatorValue("RCCUSTOMERCONTRACT_TYPE")
@DiscriminatorOptions(force = true)
@DiscriminatorColumn(length = 64)
@Table(name = "rccustomercontract")
public class RCCustomerContractImpl extends BaseModelObject implements RCCustomerContract {
  public RCCustomerContractImpl() {
    super();
  }
  
  public RCCustomerContractImpl(final RCProject project) {
    super();
    this.project = project;
  }
  
  private String contractId;
  
  public String getContractId() {
    return this.contractId;
  }
  
  public RCCustomerContract setContractId(final String contractId) {
    this.contractId = contractId;
    return this;			
    
  }
  
  @JoinColumn(name = "customer_id")
  @ManyToOne(targetEntity = RCEnterpriseImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCEnterprise customer;
  
  public RCEnterprise getCustomer() {
    return this.customer;
  }
  
  public RCCustomerContract setCustomer(final RCEnterprise customer) {
    this.customer = customer;
    return this;			
    
  }
  
  private Integer serviceYears;
  
  public Integer getServiceYears() {
    return this.serviceYears;
  }
  
  public RCCustomerContract setServiceYears(final Integer serviceYears) {
    this.serviceYears = serviceYears;
    return this;			
    
  }
  
  private Date serviceStartDate;
  
  public Date getServiceStartDate() {
    return this.serviceStartDate;
  }
  
  public RCCustomerContract setServiceStartDate(final Date serviceStartDate) {
    this.serviceStartDate = serviceStartDate;
    return this;			
    
  }
  
  @Embedded
  @AttributeOverrides(value = { @AttributeOverride(name = "isEarlyBuild", column = @Column(name = "earlyBuildInfo_isEarlyBuild") ), @AttributeOverride(name = "planContractDate", column = @Column(name = "earlyBuildInfo_planContractDate") ), @AttributeOverride(name = "acturalContractDate", column = @Column(name = "earlyBuildInfo_acturalContractDate") ), @AttributeOverride(name = "comment", column = @Column(name = "earlyBuildInfo_comment") ), @AttributeOverride(name = "workaround", column = @Column(name = "earlyBuildInfo_workaround") ) } )
  @AssociationOverrides(value = { } )
  private RCEarlyBuildInfo earlyBuildInfo = com.shrct.businessprocess.project.RCEarlyBuildInfo.create();
  
  public RCEarlyBuildInfo getEarlyBuildInfo() {
    return this.earlyBuildInfo;
  }
  
  public RCCustomerContract setEarlyBuildInfo(final RCEarlyBuildInfo earlyBuildInfo) {
    this.earlyBuildInfo = earlyBuildInfo;
    return this;			
    
  }
  
  @Column(length = 2048)
  private String comment;
  
  public String getComment() {
    return this.comment;
  }
  
  public RCCustomerContract setComment(final String comment) {
    this.comment = comment;
    return this;			
    
  }
  
  private Double serviceAmount = Double.valueOf(0d);
  
  public Double getServiceAmount() {
    return this.serviceAmount;
  }
  
  public RCCustomerContract setServiceAmount(final Double serviceAmount) {
    this.serviceAmount = serviceAmount;
    return this;			
    
  }
  
  public Double getProductAmount() {
    RCProjectMoney _totalContractAmount = this.getTotalContractAmount();
    Double _hostAmount = _totalContractAmount.toHostAmount();
    double _minus = DoubleExtensions.operator_minus(_hostAmount, this.serviceAmount);
    return Double.valueOf(_minus);
  }
  
  @OneToMany(targetEntity = RCCustomerContractMoneyAcceptImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, mappedBy = "contract")
  private Collection<RCCustomerContractMoneyAccept> moneyAccepts = new java.util.ArrayList<com.shrct.businessprocess.project.RCCustomerContractMoneyAccept>();;
  
  public Collection<RCCustomerContractMoneyAccept> getMoneyAccepts() {
    return this.moneyAccepts;
  }
  
  public RCCustomerContractMoneyAccept createAndAddtoMoneyAccepts() {
    getMoneyAccepts().size();
    com.shrct.businessprocess.project.RCCustomerContractMoneyAccept rCCustomerContractMoneyAccept = new com.shrct.businessprocess.project.impl.RCCustomerContractMoneyAcceptImpl(this);
    getObjectFactory().create(rCCustomerContractMoneyAccept);
    getMoneyAccepts().add(rCCustomerContractMoneyAccept);
    return rCCustomerContractMoneyAccept;
    
  }
  
  public RCCustomerContractMoneyAccept createAndAddtoMoneyAccepts(final Procedure1<RCCustomerContractMoneyAccept> updater) {
    getMoneyAccepts().size();
    com.shrct.businessprocess.project.RCCustomerContractMoneyAccept rCCustomerContractMoneyAccept = new com.shrct.businessprocess.project.impl.RCCustomerContractMoneyAcceptImpl(this);
    getObjectFactory().create(rCCustomerContractMoneyAccept);
    getMoneyAccepts().add(rCCustomerContractMoneyAccept);
    return rCCustomerContractMoneyAccept;
    
  }
  
  public RCCustomerContract removeAllMoneyAccepts() {
    for(RCCustomerContractMoneyAccept obj : this.moneyAccepts){
    	getObjectFactory().delete(obj);
    }
    this.moneyAccepts.clear();
    return this;	
    
  }
  
  public RCCustomerContract removeFromMoneyAccepts(final RCCustomerContractMoneyAccept rCCustomerContractMoneyAccept) {
    this.moneyAccepts.remove(rCCustomerContractMoneyAccept);
    getObjectFactory().delete(rCCustomerContractMoneyAccept);
    return this;
    
  }
  
  @JoinColumn(name = "project_id")
  @ManyToOne(targetEntity = RCProjectImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCProject project;
  
  public RCProject getProject() {
    return this.project;
  }
  
  public RCCustomerContract setProject(final RCProject project) {
    this.project = project;
    return this;			
    
  }
  
  public String getEntityName() {
    return this.contractId;
  }
  
  /**
   * 获取合同总金额
   */
  public RCProjectMoney getTotalContractAmount() {
    RCProjectMoney _xblockexpression = null;
    {
      RCCostBreakDown _cbd = this.project.getCbd();
      Collection<RCBudget> _budgets = _cbd.getBudgets();
      final Function1<RCBudget,Collection<RCBudgetContract>> _function = new Function1<RCBudget,Collection<RCBudgetContract>>() {
          public Collection<RCBudgetContract> apply(final RCBudget it) {
            Collection<RCBudgetContract> _contracts = it.getContracts();
            return _contracts;
          }
        };
      Iterable<Collection<RCBudgetContract>> _map = IterableExtensions.<RCBudget, Collection<RCBudgetContract>>map(_budgets, _function);
      Iterable<RCBudgetContract> _flatten = Iterables.<RCBudgetContract>concat(_map);
      final Function1<RCBudgetContract,Boolean> _function_1 = new Function1<RCBudgetContract,Boolean>() {
          public Boolean apply(final RCBudgetContract it) {
            RCCustomerContract _customerContract = it.getCustomerContract();
            boolean _equals = Objects.equal(_customerContract, RCCustomerContractImpl.this);
            return Boolean.valueOf(_equals);
          }
        };
      final Iterable<RCBudgetContract> contracts = IterableExtensions.<RCBudgetContract>filter(_flatten, _function_1);
      final Function1<RCBudgetContract,RCProjectMoney> _function_2 = new Function1<RCBudgetContract,RCProjectMoney>() {
          public RCProjectMoney apply(final RCBudgetContract it) {
            RCProjectMoney _totalAmount = it.getTotalAmount();
            return _totalAmount;
          }
        };
      Iterable<RCProjectMoney> _map_1 = IterableExtensions.<RCBudgetContract, RCProjectMoney>map(contracts, _function_2);
      RCExchangeRate _defaultExchangeRate = this.project.getDefaultExchangeRate();
      RCProjectMoney _sum = Moneys.sum(_map_1, _defaultExchangeRate);
      _xblockexpression = (_sum);
    }
    return _xblockexpression;
  }
  
  /**
   * 获取到款金额
   */
  public RCRealMoney getTotalAcceptedAmount() {
    RCRealMoney _xblockexpression = null;
    {
      final RCExchangeRate de = this.project.getDefaultExchangeRate();
      final Function1<RCCustomerContractMoneyAccept,RCRealMoney> _function = new Function1<RCCustomerContractMoneyAccept,RCRealMoney>() {
          public RCRealMoney apply(final RCCustomerContractMoneyAccept it) {
            RCRealMoney _acceptMoney = it.getAcceptMoney();
            return _acceptMoney;
          }
        };
      Iterable<RCRealMoney> _map = IterableExtensions.<RCCustomerContractMoneyAccept, RCRealMoney>map(this.moneyAccepts, _function);
      RCRealMoney _sumRealMoney = Moneys.sumRealMoney(_map, de);
      _xblockexpression = (_sumRealMoney);
    }
    return _xblockexpression;
  }
  
  public Double getTotalHostAcceptedAmount() {
    RCRealMoney _totalAcceptedAmount = this.getTotalAcceptedAmount();
    Double _hostAmount = _totalAcceptedAmount.toHostAmount();
    return _hostAmount;
  }
  
  /**
   * 获取开票金额
   */
  public RCProjectMoney getTotalInvoiceAmount() {
    Collection<RCPurchaseIncomeRequest> _incomeRequests = this.getIncomeRequests();
    final Function1<RCPurchaseIncomeRequest,Boolean> _function = new Function1<RCPurchaseIncomeRequest,Boolean>() {
        public Boolean apply(final RCPurchaseIncomeRequest it) {
          Boolean _isPassed = it.getIsPassed();
          return _isPassed;
        }
      };
    Iterable<RCPurchaseIncomeRequest> _filter = IterableExtensions.<RCPurchaseIncomeRequest>filter(_incomeRequests, _function);
    final Function1<RCPurchaseIncomeRequest,RCProjectMoney> _function_1 = new Function1<RCPurchaseIncomeRequest,RCProjectMoney>() {
        public RCProjectMoney apply(final RCPurchaseIncomeRequest it) {
          RCProjectMoney _amount = it.getAmount();
          return _amount;
        }
      };
    Iterable<RCProjectMoney> _map = IterableExtensions.<RCPurchaseIncomeRequest, RCProjectMoney>map(_filter, _function_1);
    RCExchangeRate _defaultExchangeRate = this.project.getDefaultExchangeRate();
    RCProjectMoney _sum = Moneys.sum(_map, _defaultExchangeRate);
    return _sum;
  }
  
  /**
   * 获取已申请开票金额
   */
  public RCProjectMoney getTotalInvoiceAndUnderInvoiceAmount() {
    Collection<RCPurchaseIncomeRequest> _incomeRequests = this.getIncomeRequests();
    final Function1<RCPurchaseIncomeRequest,Boolean> _function = new Function1<RCPurchaseIncomeRequest,Boolean>() {
        public Boolean apply(final RCPurchaseIncomeRequest it) {
          Boolean _isCanceled = it.isCanceled();
          boolean _not = (!_isCanceled);
          return Boolean.valueOf(_not);
        }
      };
    Iterable<RCPurchaseIncomeRequest> _filter = IterableExtensions.<RCPurchaseIncomeRequest>filter(_incomeRequests, _function);
    final Function1<RCPurchaseIncomeRequest,RCProjectMoney> _function_1 = new Function1<RCPurchaseIncomeRequest,RCProjectMoney>() {
        public RCProjectMoney apply(final RCPurchaseIncomeRequest it) {
          RCProjectMoney _amount = it.getAmount();
          return _amount;
        }
      };
    Iterable<RCProjectMoney> _map = IterableExtensions.<RCPurchaseIncomeRequest, RCProjectMoney>map(_filter, _function_1);
    RCExchangeRate _defaultExchangeRate = this.project.getDefaultExchangeRate();
    RCProjectMoney _sum = Moneys.sum(_map, _defaultExchangeRate);
    return _sum;
  }
  
  /**
   * 到款未分配至开票的总金额
   */
  public RCRealMoney getTotalWait4AssignmentAmount() {
    final Function1<RCCustomerContractMoneyAccept,RCRealMoney> _function = new Function1<RCCustomerContractMoneyAccept,RCRealMoney>() {
        public RCRealMoney apply(final RCCustomerContractMoneyAccept it) {
          RCRealMoney _remainMoney = it.getRemainMoney();
          return _remainMoney;
        }
      };
    Iterable<RCRealMoney> _map = IterableExtensions.<RCCustomerContractMoneyAccept, RCRealMoney>map(this.moneyAccepts, _function);
    RCExchangeRate _defaultExchangeRate = this.project.getDefaultExchangeRate();
    RCRealMoney _sumRealMoney = Moneys.sumRealMoney(_map, _defaultExchangeRate);
    return _sumRealMoney;
  }
  
  /**
   * 获取该合同总采购清单
   */
  public Collection<RCBudgetContractItem> getAllContractItems() {
    RCCostBreakDown _cbd = this.project.getCbd();
    Collection<RCBudget> _budgets = _cbd.getBudgets();
    final Function1<RCBudget,Collection<RCBudgetContract>> _function = new Function1<RCBudget,Collection<RCBudgetContract>>() {
        public Collection<RCBudgetContract> apply(final RCBudget it) {
          Collection<RCBudgetContract> _contracts = it.getContracts();
          return _contracts;
        }
      };
    Iterable<Collection<RCBudgetContract>> _map = IterableExtensions.<RCBudget, Collection<RCBudgetContract>>map(_budgets, _function);
    Iterable<RCBudgetContract> _flatten = Iterables.<RCBudgetContract>concat(_map);
    final Function1<RCBudgetContract,Boolean> _function_1 = new Function1<RCBudgetContract,Boolean>() {
        public Boolean apply(final RCBudgetContract it) {
          RCCustomerContract _customerContract = it.getCustomerContract();
          boolean _equals = Objects.equal(_customerContract, RCCustomerContractImpl.this);
          return Boolean.valueOf(_equals);
        }
      };
    Iterable<RCBudgetContract> _filter = IterableExtensions.<RCBudgetContract>filter(_flatten, _function_1);
    final Function1<RCBudgetContract,Collection<RCBudgetContractItem>> _function_2 = new Function1<RCBudgetContract,Collection<RCBudgetContractItem>>() {
        public Collection<RCBudgetContractItem> apply(final RCBudgetContract it) {
          Collection<RCBudgetContractItem> _contratItems = it.getContratItems();
          return _contratItems;
        }
      };
    Iterable<Collection<RCBudgetContractItem>> _map_1 = IterableExtensions.<RCBudgetContract, Collection<RCBudgetContractItem>>map(_filter, _function_2);
    Iterable<RCBudgetContractItem> _flatten_1 = Iterables.<RCBudgetContractItem>concat(_map_1);
    List<RCBudgetContractItem> _list = IterableExtensions.<RCBudgetContractItem>toList(_flatten_1);
    return _list;
  }
  
  public Iterable<RCBudgetContract> getAllBudgetContract() {
    RCCostBreakDown _cbd = this.project.getCbd();
    Collection<RCBudget> _budgets = _cbd.getBudgets();
    final Function1<RCBudget,Collection<RCBudgetContract>> _function = new Function1<RCBudget,Collection<RCBudgetContract>>() {
        public Collection<RCBudgetContract> apply(final RCBudget it) {
          Collection<RCBudgetContract> _contracts = it.getContracts();
          return _contracts;
        }
      };
    Iterable<Collection<RCBudgetContract>> _map = IterableExtensions.<RCBudget, Collection<RCBudgetContract>>map(_budgets, _function);
    Iterable<RCBudgetContract> _flatten = Iterables.<RCBudgetContract>concat(_map);
    final Function1<RCBudgetContract,Boolean> _function_1 = new Function1<RCBudgetContract,Boolean>() {
        public Boolean apply(final RCBudgetContract it) {
          RCCustomerContract _customerContract = it.getCustomerContract();
          boolean _equals = Objects.equal(_customerContract, RCCustomerContractImpl.this);
          return Boolean.valueOf(_equals);
        }
      };
    Iterable<RCBudgetContract> _filter = IterableExtensions.<RCBudgetContract>filter(_flatten, _function_1);
    return _filter;
  }
  
  /**
   * 在该客户合同为项目唯一合同的情况下，将预算信息中的销售收入直接分配给该合同
   */
  public void populateToAllBudget() {
    RCCostBreakDown _cbd = this.project.getCbd();
    Collection<RCBudget> _budgets = _cbd.getBudgets();
    final Procedure1<RCBudget> _function = new Procedure1<RCBudget>() {
        public void apply(final RCBudget budget) {
          Collection<RCBudgetContract> _contracts = budget.getContracts();
          final Procedure1<RCBudgetContract> _function = new Procedure1<RCBudgetContract>() {
              public void apply(final RCBudgetContract it) {
                RCProjectMoney _contractPrice = budget.getContractPrice();
                it.setAmount(_contractPrice);
              }
            };
          IterableExtensions.<RCBudgetContract>forEach(_contracts, _function);
        }
      };
    IterableExtensions.<RCBudget>forEach(_budgets, _function);
  }
  
  public Collection<RCPurchaseIncomeRequest> getIncomeRequests() {
    IGenericQuery<RCPurchaseIncomeRequest> query=getObjectFactory().createGenericQuery(RCPurchaseIncomeRequest.class,"select ir from com.shrct.businessprocess.purchase.impl.RCPurchaseIncomeRequestImpl as ir   where ((ir.customerContract) = (:this)) and  ir.active = 1      ").setParameter("this",this);
    Collection<RCPurchaseIncomeRequest> _list = query.list();
    return _list;
  }
  
  /**
   * 累计开票销项税
   */
  public Double getIncomeTax() {
    Collection<RCPurchaseIncomeRequest> _incomeRequests = this.getIncomeRequests();
    final Function1<RCPurchaseIncomeRequest,Boolean> _function = new Function1<RCPurchaseIncomeRequest,Boolean>() {
        public Boolean apply(final RCPurchaseIncomeRequest it) {
          Boolean _isPassed = it.getIsPassed();
          return _isPassed;
        }
      };
    Iterable<RCPurchaseIncomeRequest> _filter = IterableExtensions.<RCPurchaseIncomeRequest>filter(_incomeRequests, _function);
    final Function1<RCPurchaseIncomeRequest,Double> _function_1 = new Function1<RCPurchaseIncomeRequest,Double>() {
        public Double apply(final RCPurchaseIncomeRequest it) {
          Double _actualTax = it.getActualTax();
          return _actualTax;
        }
      };
    Iterable<Double> _map = IterableExtensions.<RCPurchaseIncomeRequest, Double>map(_filter, _function_1);
    final Function2<Double,Double,Double> _function_2 = new Function2<Double,Double,Double>() {
        public Double apply(final Double d1, final Double d2) {
          double _plus = DoubleExtensions.operator_plus(d1, d2);
          return Double.valueOf(_plus);
        }
      };
    Double _reduce = IterableExtensions.<Double>reduce(_map, _function_2);
    Double _nullsafe = NumberExtensions.nullsafe(_reduce);
    return _nullsafe;
  }
  
  public Iterable<RCProductEntry> getAllAcceptedProducts() {
    Iterable<RCProductEntry> _xblockexpression = null;
    {
      RCCostBreakDown _cbd = this.project.getCbd();
      Collection<RCBudget> _budgets = _cbd.getBudgets();
      Iterable<RCBaseProductPurchaseBudget> _filter = Iterables.<RCBaseProductPurchaseBudget>filter(_budgets, RCBaseProductPurchaseBudget.class);
      final Function1<RCBaseProductPurchaseBudget,Collection<RCBudgetContract>> _function = new Function1<RCBaseProductPurchaseBudget,Collection<RCBudgetContract>>() {
          public Collection<RCBudgetContract> apply(final RCBaseProductPurchaseBudget it) {
            Collection<RCBudgetContract> _contracts = it.getContracts();
            return _contracts;
          }
        };
      Iterable<Collection<RCBudgetContract>> _map = IterableExtensions.<RCBaseProductPurchaseBudget, Collection<RCBudgetContract>>map(_filter, _function);
      Iterable<RCBudgetContract> _flatten = Iterables.<RCBudgetContract>concat(_map);
      final Function1<RCBudgetContract,Boolean> _function_1 = new Function1<RCBudgetContract,Boolean>() {
          public Boolean apply(final RCBudgetContract it) {
            RCCustomerContract _customerContract = it.getCustomerContract();
            boolean _equals = Objects.equal(_customerContract, RCCustomerContractImpl.this);
            return Boolean.valueOf(_equals);
          }
        };
      final Iterable<RCBudgetContract> contracts = IterableExtensions.<RCBudgetContract>filter(_flatten, _function_1);
      final Function1<RCBudgetContract,Iterable<RCProductEntry>> _function_2 = new Function1<RCBudgetContract,Iterable<RCProductEntry>>() {
          public Iterable<RCProductEntry> apply(final RCBudgetContract it) {
            Iterable<RCProductEntry> _allAcceptedProducts = it.getAllAcceptedProducts();
            return _allAcceptedProducts;
          }
        };
      Iterable<Iterable<RCProductEntry>> _map_1 = IterableExtensions.<RCBudgetContract, Iterable<RCProductEntry>>map(contracts, _function_2);
      Iterable<RCProductEntry> _flatten_1 = Iterables.<RCProductEntry>concat(_map_1);
      final Function1<RCProductEntry,Boolean> _function_3 = new Function1<RCProductEntry,Boolean>() {
          public Boolean apply(final RCProductEntry it) {
            RCEnterprise _enterprise = it.getEnterprise();
            boolean _equals = Objects.equal(_enterprise, RCCustomerContractImpl.this.customer);
            return Boolean.valueOf(_equals);
          }
        };
      Iterable<RCProductEntry> _filter_1 = IterableExtensions.<RCProductEntry>filter(_flatten_1, _function_3);
      _xblockexpression = (_filter_1);
    }
    return _xblockexpression;
  }
  
  public Iterable<RCIdentifiablePhysicalProductEntry> getAllTracingProducts() {
    Set<RCIdentifiablePhysicalProductEntry> _xblockexpression = null;
    {
      RCCostBreakDown _cbd = this.project.getCbd();
      Collection<RCBudget> _budgets = _cbd.getBudgets();
      Iterable<RCHasPurchaseBudget> _filter = Iterables.<RCHasPurchaseBudget>filter(_budgets, RCHasPurchaseBudget.class);
      final Function1<RCHasPurchaseBudget,Collection<RCBudgetContract>> _function = new Function1<RCHasPurchaseBudget,Collection<RCBudgetContract>>() {
          public Collection<RCBudgetContract> apply(final RCHasPurchaseBudget it) {
            Collection<RCBudgetContract> _contracts = it.getContracts();
            return _contracts;
          }
        };
      Iterable<Collection<RCBudgetContract>> _map = IterableExtensions.<RCHasPurchaseBudget, Collection<RCBudgetContract>>map(_filter, _function);
      Iterable<RCBudgetContract> _flatten = Iterables.<RCBudgetContract>concat(_map);
      final Function1<RCBudgetContract,Boolean> _function_1 = new Function1<RCBudgetContract,Boolean>() {
          public Boolean apply(final RCBudgetContract it) {
            RCCustomerContract _customerContract = it.getCustomerContract();
            boolean _equals = Objects.equal(_customerContract, RCCustomerContractImpl.this);
            return Boolean.valueOf(_equals);
          }
        };
      final Iterable<RCBudgetContract> contracts = IterableExtensions.<RCBudgetContract>filter(_flatten, _function_1);
      final Function1<RCBudgetContract,Iterable<RCIdentifiablePhysicalProductEntry>> _function_2 = new Function1<RCBudgetContract,Iterable<RCIdentifiablePhysicalProductEntry>>() {
          public Iterable<RCIdentifiablePhysicalProductEntry> apply(final RCBudgetContract it) {
            Iterable<RCIdentifiablePhysicalProductEntry> _xblockexpression = null;
            {
              RCBudget _budget = it.getBudget();
              final RCHasPurchaseBudget budget = ((RCHasPurchaseBudget) _budget);
              Iterable<RCIdentifiablePhysicalProductEntry> _allTracingProducts = RCCustomerContractImpl.this.getAllTracingProducts(budget);
              _xblockexpression = (_allTracingProducts);
            }
            return _xblockexpression;
          }
        };
      Iterable<Iterable<RCIdentifiablePhysicalProductEntry>> _map_1 = IterableExtensions.<RCBudgetContract, Iterable<RCIdentifiablePhysicalProductEntry>>map(contracts, _function_2);
      Iterable<RCIdentifiablePhysicalProductEntry> _flatten_1 = Iterables.<RCIdentifiablePhysicalProductEntry>concat(_map_1);
      final Set<RCIdentifiablePhysicalProductEntry> result = IterableExtensions.<RCIdentifiablePhysicalProductEntry>toSet(_flatten_1);
      _xblockexpression = (result);
    }
    return _xblockexpression;
  }
  
  public Iterable<RCIdentifiablePhysicalProductEntry> getAllTracingProducts(final RCHasPurchaseBudget budget) {
    Iterable<RCIdentifiablePhysicalProductEntry> _xblockexpression = null;
    {
      final RCEnterprise customer = this.customer;
      IGenericQuery<RCProductServiceTracer> query=getObjectFactory().createGenericQuery(RCProductServiceTracer.class,"select e from com.shrct.businessprocess.product.impl.RCProductServiceTracerImpl as e   where (((e.budgetContract.budget) = (:budget)) and ((e.product.enterprise) = (:customer))) and  e.active = 1      ").setParameter("budget",budget).setParameter("customer",customer);
      Collection<RCProductServiceTracer> _list = query.list();
      final Function1<RCProductServiceTracer,RCIdentifiablePhysicalProductEntry> _function = new Function1<RCProductServiceTracer,RCIdentifiablePhysicalProductEntry>() {
          public RCIdentifiablePhysicalProductEntry apply(final RCProductServiceTracer it) {
            RCIdentifiablePhysicalProductEntry _product = it.getProduct();
            return _product;
          }
        };
      Iterable<RCIdentifiablePhysicalProductEntry> _map = IterableExtensions.<RCProductServiceTracer, RCIdentifiablePhysicalProductEntry>map(_list, _function);
      _xblockexpression = (_map);
    }
    return _xblockexpression;
  }
  
  public Iterable<RCIdentifiablePhysicalProductEntry> getAllTracingProducts(final RCBudgetContract budgetContract) {
    Iterable<RCIdentifiablePhysicalProductEntry> _xblockexpression = null;
    {
      final RCEnterprise customer = this.customer;
      IGenericQuery<RCProductServiceTracer> query=getObjectFactory().createGenericQuery(RCProductServiceTracer.class,"select e from com.shrct.businessprocess.product.impl.RCProductServiceTracerImpl as e   where (((e.budgetContract) = (:budgetContract)) and ((e.product.enterprise) = (:customer))) and  e.active = 1      ").setParameter("budgetContract",budgetContract).setParameter("customer",customer);
      Collection<RCProductServiceTracer> _list = query.list();
      final Function1<RCProductServiceTracer,RCIdentifiablePhysicalProductEntry> _function = new Function1<RCProductServiceTracer,RCIdentifiablePhysicalProductEntry>() {
          public RCIdentifiablePhysicalProductEntry apply(final RCProductServiceTracer it) {
            RCIdentifiablePhysicalProductEntry _product = it.getProduct();
            return _product;
          }
        };
      Iterable<RCIdentifiablePhysicalProductEntry> _map = IterableExtensions.<RCProductServiceTracer, RCIdentifiablePhysicalProductEntry>map(_list, _function);
      _xblockexpression = (_map);
    }
    return _xblockexpression;
  }
  
  public RCCustomerContractMoneyAccept moneyAccepted(final Procedure1<? super RCCustomerContractMoneyAccept> init) {
    RCCustomerContractMoneyAccept _createAndAddtoMoneyAccepts = this.createAndAddtoMoneyAccepts();
    final Procedure1<RCCustomerContractMoneyAccept> _function = new Procedure1<RCCustomerContractMoneyAccept>() {
        public void apply(final RCCustomerContractMoneyAccept it) {
          init.apply(it);
          RCProjectStat _stat = RCCustomerContractImpl.this.project.getStat();
          _stat.updateInfo();
        }
      };
    RCCustomerContractMoneyAccept _doubleArrow = ObjectExtensions.<RCCustomerContractMoneyAccept>operator_doubleArrow(_createAndAddtoMoneyAccepts, _function);
    return _doubleArrow;
  }
  
  public void diagnose(final IDiagnoseAcceptor acceptor) {
  }
  
  public static RCCustomerContract create(final RCProject project, final IObjectFactory objectFactory) {
    com.shrct.businessprocess.project.RCCustomerContract rCCustomerContract = new com.shrct.businessprocess.project.impl.RCCustomerContractImpl(
    	project
    );
    objectFactory.create(rCCustomerContract);
    return rCCustomerContract;			
    
  }
  
  public static RCCustomerContract createRCCustomerContractImpl(final RCProject project, final IObjectFactory objectFactory, final Procedure1<RCCustomerContract> init) {
    com.shrct.businessprocess.project.RCCustomerContract rCCustomerContract = new com.shrct.businessprocess.project.impl.RCCustomerContractImpl(
    	project
    );
    objectFactory.create(rCCustomerContract);
    init.apply(rCCustomerContract);
    
    return rCCustomerContract;			
    
  }
  
  public RCCustomerContractProxy toProxy() {
    com.shrct.businessprocess.project.proxy.RCCustomerContractProxy proxy = new com.shrct.businessprocess.project.proxy.RCCustomerContractProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
